//
//  DZDropMenu.m
//  Weico
//
//  Created by 大泽 on 15/7/21.
//  Copyright (c) 2015年 刘洪泽. All rights reserved.
//

#import "DZDropMenu.h"
#import "DZDropDownViewController.h"
@interface DZDropMenu ()

@property (nonatomic, weak) UIImageView *containerView;

@end

@implementation DZDropMenu

- (UIImageView *)containerView
{
    if (!_containerView) {
        
        UIImageView *containerView = [[UIImageView alloc] init];
        containerView.image = [UIImage imageNamed:@"popover_background"];
        containerView.userInteractionEnabled = YES;
        containerView.alpha = 0.8;
        [self addSubview:containerView];
        _containerView = containerView;
        
    }
    return _containerView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


+ (instancetype)showFrom:(UIView *)from
{
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    
    DZDropMenu *cover = [[DZDropMenu alloc] init];
    cover.frame = window.bounds;
    [window addSubview:cover];
    
    [cover showMenuFrom:from];
    
    return cover;
}
- (void)showMenuFrom:(UIView *)from
{
    self.containerView.width = 160;
    self.containerView.height = 200;
    
    CGRect newFrame = [from convertRect:from.bounds toView:[[UIApplication sharedApplication].windows lastObject]];
    
    self.containerView.centerX = CGRectGetMidX(newFrame);
    self.containerView.y = CGRectGetMaxY(newFrame);
    
}

- (void)setContentVc:(UIViewController *)contentVc
{
    _contentVc = contentVc;
    
    contentVc.view.x = 10;
    contentVc.view.y = 15;
    contentVc.view.width = self.containerView.width - 20;
    self.containerView.height = CGRectGetMaxY(contentVc.view.frame) + 10;
    [self.containerView addSubview:contentVc.view];
    
    if ([self.delegate respondsToSelector:@selector(dropMenuDidShow:)]) {
        [self.delegate dropMenuDidShow:self];
    }
    
}

- (void)dismiss
{
    if ([self.delegate respondsToSelector:@selector(dropMenuDidDissmiss:)]) {
        [self.delegate dropMenuDidDissmiss:self];
    }
    
    [self removeFromSuperview];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}




@end
